import {
  addDevelopRuleApi,
  getProgramOptions,
  updateDevelopRuleApi,
} from '@/services/api';
import {
  DrawerForm,
  ProFormDigit,
  ProFormInstance,
  ProFormSelect,
} from '@ant-design/pro-components';
import { useRequest } from '@umijs/max';
import { Form, message } from 'antd';
import React, { useEffect, useRef } from 'react';

interface Props {
  open?: boolean;
  info?: any;
  close?: () => void;
  onFinsh?: () => void;
}
// 新增/编辑用户
const ClientDrawer: React.FC<Props> = ({ open, close, info,onFinsh }) => {
  const [form] = Form.useForm<{ name: string; company: string }>();

  const formRef = useRef<ProFormInstance>();

  useEffect(() => {
    if (info) {
      setTimeout(() => {
        formRef?.current?.setFieldsValue({
          ...info,
        });
      }, 20);
    }
  }, [info]);

  // 查询所有商户
  const { data: merchantList } = useRequest(async () => {
    return await getProgramOptions();
  });

  return (
    <DrawerForm
      formRef={formRef}
      title={info.id ? '编辑用户' : '新建用户'}
      form={form}
      open={open}
      width={'38%'}
      onOpenChange={(visible) => {
        if (!visible) {
          close?.();
        }
      }}
      autoFocusFirstInput
      drawerProps={{
        destroyOnClose: true,
      }}
      onFinish={async (values) => { 

        if (info?.id) {
          try {
            await updateDevelopRuleApi(info?.id, {
              ...values,
            });
            onFinsh?.()
            message.success('添加成功');
            // 不返回不会关闭弹框
            return true;
          } catch (error) {
            return false;
          }
        } else {
          try {
            await addDevelopRuleApi({
              ...values,
            });

            message.success('更新成功');
            // 不返回不会关闭弹框
            return true;
          } catch (error) {
            // message.success('提交失败');
            return false;
          }
        }
      }}
    >
      <ProFormSelect
        name="user"
        label="商户关联"
        disabled={!!info?.id}
        rules={[
          {
            required: true,
          },
        ]}
        options={merchantList ?? []}
      ></ProFormSelect>

      <ProFormDigit
        label="半月奖励"
        name="half"
        rules={[
          {
            required: true,
          },
        ]}
        fieldProps={{
          type: 'number',
        }}
      />

      <ProFormDigit
        label="包月奖励"
        name="subscription"
        rules={[
          {
            required: true,
          },
        ]}
        fieldProps={{
          type: 'number',
        }}
      />

      <ProFormDigit
        label="按次奖励"
        name="sequence"
        rules={[
          {
            required: true,
          },
        ]}
        fieldProps={{
          type: 'number',
        }}
      />

      <ProFormDigit
        label="车电一体奖励"
        name="integrated"
        rules={[
          {
            required: true,
          },
        ]}
        fieldProps={{
          type: 'number',
        }}
      />
    </DrawerForm>
  );
};

export default ClientDrawer;
